package com.byx.chatclient.presenter.home;

import android.content.Context;

import com.byx.chatclient.adapter.ChatAdapter;
import com.byx.chatclient.contract.home.ChatTaskContract;
import com.byx.chatclient.entity.MessagePo;
import com.byx.chatclient.entity.message.AudioMsgBody;
import com.byx.chatclient.entity.message.ImageMsgBody;
import com.byx.chatclient.entity.message.Message;
import com.byx.chatclient.entity.message.MsgBody;
import com.byx.chatclient.entity.message.MsgType;
import com.byx.chatclient.entity.message.TextMsgBody;
import com.byx.chatclient.entity.message.VideoMsgBody;
import com.byx.chatclient.im.AbstractMessageSender;
import com.byx.chatclient.im.MessageBuilder;
import com.byx.chatclient.im.MessageConverter;
import com.byx.chatclient.model.ChatModel;
import com.byx.chatclient.presenter.BasePresenter;

import java.util.List;

/**
 * ChatPresenter
 *
 * @author Superb
 * @date 2021/3/27 21:57
 */
public class ChatPresenter<T extends ChatTaskContract.View> extends BasePresenter<T> implements ChatTaskContract.Presenter {
    private ChatModel mChatModel;
    private ChatAdapter mChatAdapter;
    private boolean isSingleChat;
    private final AbstractMessageSender messageSender;

    public ChatPresenter(Context context,boolean isSingleChat) {
        this.mChatModel = new ChatModel(context);
        this.isSingleChat = isSingleChat;
        this.messageSender = new AbstractMessageSender() {
            @Override
            public Message buildMessage(MsgBody body, String senderId, String targetId) {
                Message message;
                if (body instanceof TextMsgBody){
                    message = MessageBuilder.getBaseSendMessage(MsgType.TEXT, senderId, targetId);
                }else {
                    if (body instanceof ImageMsgBody){
                        message = MessageBuilder.getBaseSendMessage(MsgType.IMAGE, senderId, targetId);
                    }else if (body instanceof VideoMsgBody){
                        message = MessageBuilder.getBaseSendMessage(MsgType.VIDEO, senderId, targetId);
                    }else if (body instanceof AudioMsgBody){
                        message = MessageBuilder.getBaseSendMessage(MsgType.AUDIO, senderId, targetId);
                    }else {
                        message = MessageBuilder.getBaseSendMessage(MsgType.FILE, senderId, targetId);
                    }
                }
                message.setBody(body);
                return message;
            }

            @Override
            public void updateView(Message message) {
                isActive();
                saveChatMsg(MessageConverter.toMessagePo(message,isSingleChat));
                view.updateMsg(message);
            }

            /**
             * 更新多个消息
             *
             * @param messages
             */
            @Override
            public void updateViews(List<Message> messages) {
                for (Message message : messages){
                    updateView(message);
                }
            }
        };
    }

    public void setChatAdapter(ChatAdapter chatAdapter) {
        this.mChatAdapter = chatAdapter;
    }

    @Override
    public List<MessagePo> getSingleChatMsg(String userId, String friendId) {
        return mChatModel.getChatMessage(userId, friendId);
    }

    @Override
    public List<MessagePo> getGroupChatMsg(String groupId) {
        return mChatModel.getGroupChatMsg(groupId);
    }

    @Override
    public void saveChatMsg(MessagePo messagePo) {
        mChatModel.saveChatMessage(messagePo);
    }

    @Override
    public void sendTextMessage(AbstractMessageSender.TextMessageParam messageParam) {
        messageSender.sendTextMessage(messageParam);
    }

    @Override
    public void sendFileMessage(AbstractMessageSender.FileMessageParam messageParam, AbstractMessageSender.OnUploadListener onUploadListener) {
        messageSender.sendFileMessage(messageParam,onUploadListener);
    }

    public void deleteMsg(String msgId){
        mChatModel.deleteOneMessage(msgId);
    }

}
